package com.kamistoat.meimeistore.member.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.alibaba.fastjson.JSON;
import com.kamistoat.common.To.RegisterMemberTo;
import com.kamistoat.common.exception.ALLRunTimeException;
import com.kamistoat.common.exception.BizCodeEnum;
import com.kamistoat.meimeistore.member.entity.MemberLoginLogEntity;
import com.kamistoat.meimeistore.member.service.MemberLoginLogService;
import com.kamistoat.meimeistore.member.entity.MemberEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.kamistoat.meimeistore.member.service.MemberService;
import com.kamistoat.common.utils.PageUtils;
import com.kamistoat.common.utils.R;


/**
 * 会员
 *
 * @author kamistoat
 * @email linjianshanke@163.com
 * @date 2021-09-12 16:36:46
 */
@RestController
@RequestMapping("meimeistoremember/member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private MemberLoginLogService memberLoginLogServicel;

    /**
     * 自己写的接口，用于注册用户信息
     *
     * @param registerMemberTo auth模块传来的To，包含四个变量，这里会映射成三个变量
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody RegisterMemberTo registerMemberTo) {
        // 获取可能抛出的手机号不唯一异常
        try {
            memberService.registerMember(registerMemberTo);
        } catch (ALLRunTimeException.PhoneNumUniqueException e) {
            return R.error(BizCodeEnum.PHONENUM_EXIST_EXCEPTION.getCode(), BizCodeEnum.PHONENUM_EXIST_EXCEPTION.getMessage());
        } catch (Exception e) {
            // 抛出的其他异常
            return R.error(BizCodeEnum.UNKNOW_EXCEPTION.getCode(), e.getMessage());
        }
        return R.ok();
    }

    /**
     * 自己写的接口。登录
     *
     * @param loginAccount 登录账号
     * @param password     登录密码
     * @param IP           登录IP
     * @param IPAddr       登录Ip定位
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestParam String loginAccount, @RequestParam String password,
                   @RequestParam String IP, @RequestParam String IPAddr) {
        R loginState = memberService.login(loginAccount, password);
        // 登录成功同时保存登录日志
        if ((int) loginState.get("code") == 0) {
            MemberEntity memberEntity = JSON.parseObject((String) loginState.get("data"), MemberEntity.class);
            MemberLoginLogEntity loginLog = new MemberLoginLogEntity();
            loginLog.setMemberId(memberEntity.getId());
            loginLog.setCreateTime(new Date());
            loginLog.setLoginType(1);
            loginLog.setIp(IP);
            loginLog.setCity(IPAddr);
            memberLoginLogServicel.save(loginLog);
        }
        return loginState;
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("meimeistoremember:member:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("meimeistoremember:member:info")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("meimeistoremember:member:save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("meimeistoremember:member:update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("meimeistoremember:member:delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
